package com.yxt.function_sdk_share;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.md.basedpc.PHSizeUtils;
import com.md.livingwidget.uiutils.ShapeUtils;
import com.yxt.function_sdk_share.constants.SHARE_TYPE;
import com.yxt.function_sdk_share.constants.SINGLE_AUDIO_TYPE;

import io.reactivex.rxjava3.core.SingleEmitter;

/**
 * 项目名称：com.yxt.function_sdk_share
 * 类名称：SharePopuWindow
 * 类描述：分享弹框
 * 创建人：wuht
 * 创建时间：2019-12-03 19:11
 */
public class BottomPopupWindow extends PopupWindow {


    private View mMenuView;
    /***
     * popuwindow分享弹框
     * @param activity
     * @param share_types
     * @param popupWindowItemClick
     */
    public BottomPopupWindow(Activity activity, SINGLE_AUDIO_TYPE[] share_types, PopupWindowItemClick popupWindowItemClick) {
        super(activity);
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.alert_dialog_share_yxtsdk, null);
        HorizontalScrollView hs_share_outer = mMenuView.findViewById(R.id.hs_share_outer);
        ConstraintLayout top_ll = mMenuView.findViewById(R.id.top_ll);
        LinearLayout ll_share_outer = mMenuView.findViewById(R.id.ll_share_outer);
        HorizontalScrollView hs_share_inner = mMenuView.findViewById(R.id.hs_share_inner);
        LinearLayout ll_share_inner = mMenuView.findViewById(R.id.ll_share_inner);
        View line = mMenuView.findViewById(R.id.empty_view2);
        if (share_types == null) {
            share_types = new SINGLE_AUDIO_TYPE[]{SINGLE_AUDIO_TYPE.ADD, SINGLE_AUDIO_TYPE.DELETE};
        }
        for (int i = 0; i < share_types.length; i++) {
            View itemView = inflater.inflate(R.layout.custom_item_share_yxtsdk, null);
            ImageView im_share_icon = itemView.findViewById(R.id.im_share_icon);
            TextView tv_share_text = itemView.findViewById(R.id.tv_share_text);
            im_share_icon.setImageResource(share_types[i].getDrawableId());
            tv_share_text.setText(activity.getResources().getString(share_types[i].getLabelId()));
            
            itemView.setOnClickListener(new ShareClickListener(i, share_types[i], popupWindowItemClick));
            
            double width = PHSizeUtils.getScreenWidth(activity) * (1 - 0.1074) / 4;
            LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams((int) width, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            if (share_types[i].getIsOuterShare()==1) {
                ll_share_outer.addView(itemView, parm);
                hs_share_outer.setVisibility(View.VISIBLE);
            } else if (share_types[i].getIsOuterShare() == 2) {
                ll_share_inner.addView(itemView, parm);
                hs_share_inner.setVisibility(View.VISIBLE);
            }
        }
        boolean isSingleLine = ll_share_inner.getChildCount() == 0 || ll_share_outer.getChildCount() == 0;
        line.setVisibility(isSingleLine ? View.GONE : View.VISIBLE);
        
        mMenuView.findViewById(R.id.btn_share_cancel).setOnClickListener(v -> dismiss());

        Drawable d = ShapeUtils.getDrawableList(PHSizeUtils.dp2px(activity, 1), PHSizeUtils.dp2px(activity, 16),
                activity.getResources().getColor(R.color.white), activity.getResources().getColor(R.color.white));

        // Drawable d = ShapeUtils.getDrawableList(0, PHSizeUtils.dp2px(activity, 16), PHSizeUtils.dp2px(activity, 16), 0, 0, 0, R.color.un_press_color);
    
    
        top_ll.setBackgroundDrawable(d);
        
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.Share_Animation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // this.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.shape_round_rect));
        // this.setOutsideTouchable(false);
    
        setBackgroundAlpha(activity, 0.6f);
        setOnDismissListener(() -> setBackgroundAlpha(activity, 1.0f));
    }
    
    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }

    /***
     * 分享点击回调
     */
    class ShareClickListener implements View.OnClickListener {
        PopupWindowItemClick popupWindowItemClick;
        int pos;
        SINGLE_AUDIO_TYPE share_type;

        public ShareClickListener(int pos, SINGLE_AUDIO_TYPE share_type, PopupWindowItemClick popupWindowItemClick) {
            this.popupWindowItemClick = popupWindowItemClick;
            this.pos = pos;
            this.share_type = share_type;
        }

        @Override
        public void onClick(View v) {
            if (popupWindowItemClick != null) {
                popupWindowItemClick.clickCallBack(pos, share_type);
            }
            dismiss();
        }
    }


    /***
     * popuwindow点击回调
     */
    public interface PopupWindowItemClick {
        void clickCallBack(int pos, SINGLE_AUDIO_TYPE share_type);
    }


    /***
     * dip转px
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}
